package com.hsck.order.kds.main.fragment.record

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.hsck.order.kds.base.isnoam.log.XLog
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.base.utils.NetworkUtil
import com.hsck.order.kds.databinding.FragmentFoodDeliveryRecordBinding
import com.hsck.order.kds.main.dialog.SelectNumberDialog
import com.hsck.order.kds.main.dialog.SelectQuantityDialog
import com.hsck.order.kds.main.fragment.record.dialog.ConfirmDishDialog
import com.hsck.order.kds.main.fragment.record.dialog.DishSuccessDialog
import com.hsck.order.kds.main.fragment.standard.bean.DishManager
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean
import com.hsck.order.kds.main.itf.UpdatableFragmentListener

/**
 * 传菜记录
 */
class FoodDeliveryRecordFragment : BaseFragment<FragmentFoodDeliveryRecordBinding>(),
    UpdatableFragmentListener {

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentFoodDeliveryRecordBinding {
        return FragmentFoodDeliveryRecordBinding.inflate(inflater)
    }

    private var foodDeliveryRecordAdapter: FoodDeliveryRecordAdapter? = null
    private var selectQuantityDialog: SelectQuantityDialog? = null
    private var selectNumberDialog: SelectNumberDialog? = null
    private var itemBean: TestListBean? = null
    private var position: Int? = null
    private var confirmDishDialog: ConfirmDishDialog? = null
    private var dishSuccessDialog:DishSuccessDialog? = null


//    override fun loadDataOnce() {
//        super.loadDataOnce()
////        initRecyclerView()
//        showNoDataView {  }
//        foodDeliveryRecordAdapter?.submitList(DishManager().getDishList())
//        binding.flRoot.visibility = View.VISIBLE
//    }

    override fun onResume() {
        super.onResume()
//        initRecyclerView()
        showNoDataView {  }
        foodDeliveryRecordAdapter?.submitList(DishManager().getDishList())
        binding.flRoot.visibility = View.VISIBLE
    }

    private fun initRecyclerView() {
        foodDeliveryRecordAdapter = FoodDeliveryRecordAdapter(requireActivity(), 3)
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = foodDeliveryRecordAdapter
        layoutManager.isItemPrefetchEnabled = true
        binding.recyclerView.setItemViewCacheSize(20)
        binding.recyclerView.setHasFixedSize(true)
        foodDeliveryRecordAdapter!!.setRestoreDishListener(object :
            FoodDeliveryRecordAdapter.RestoreDishListener {
            override fun onRestoreDishClick(itemBean: TestListBean, position: Int) {
                //取消划菜
                this@FoodDeliveryRecordFragment.itemBean = itemBean
                this@FoodDeliveryRecordFragment.position = position
                if (confirmDishDialog == null) {
                    confirmDishDialog = ConfirmDishDialog(requireActivity())
                }
                confirmDishDialog!!.show()
                confirmDishDialog!!.setConfirmDishListener(object :
                    ConfirmDishDialog.ConfirmDishListener {
                    override fun onConfirmDishClick() {
                        foodDeliveryRecordAdapter?.removeItemAfterIndex(position)
                        showDishSuccess()
                    }
                })
            }
        })
    }

    private fun showDishSuccess() {
        confirmDishDialog!!.dismiss()
        if (dishSuccessDialog == null) {
            dishSuccessDialog = DishSuccessDialog(requireActivity())
        }
        dishSuccessDialog!!.show()
    }

    /**
     * 数量大于2，设置划菜数量弹窗
     */
    private fun showNumberDialog() {
        if (selectNumberDialog == null) {
            selectNumberDialog = activity?.let { SelectNumberDialog(it) }
        }
        selectNumberDialog!!.show("测试数据", itemBean?.number.toString())
        selectNumberDialog!!.setSelectNumberListener(object :
            SelectNumberDialog.SelectNumberListener {
            override fun onSelectNumber(i: Int) {
                XLog.i("选择的数量为：$i")
            }
        })
    }

    /**
     * 1、2份数弹窗
     */
    private fun showQuantityDialog() {
        if (selectQuantityDialog == null) {
            selectQuantityDialog = activity?.let { SelectQuantityDialog(it) }
        }
        selectQuantityDialog!!.show("测试数据")
        selectQuantityDialog!!.setSelectQuantityListener(object :
            SelectQuantityDialog.SelectQuantityListener {
            override fun onSelectQuantity(i: Int) {
                XLog.i("选择的数量为：$i")
            }
        })
    }

    override fun onFragmentVisible() {
        if (_binding != null) {
            if (NetworkUtil.isNetworkAvailable(requireActivity())) {
                hideLoadErrorView()
                foodDeliveryRecordAdapter?.submitList(DishManager().getDishList())
                binding.flRoot.visibility = View.VISIBLE
            } else {
                showLoadErrorView {
                    XLog.i("加载失败，请点击重试")
                }
            }
        }
    }

    override fun onFragmentInvisible() {
        binding.flRoot.visibility = View.GONE
        //停止接收MQTT
    }

}